//
//  FirstViewController.m
//  YouRBook
//
//  Created by allting on 12. 9. 15..
//  Copyright (c) 2012년 allting. All rights reserved.
//

#import "FirstViewController.h"

#define kPOPULAR_URL		@"http://clp.hoksi.wo.tc/feed/lists/global/"

@interface FirstViewController ()
@end

@implementation FirstViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
		self.title = NSLocalizedString(@"인기", @"인기");
		self.tabBarItem.image = [UIImage imageNamed:@"112-group"];
//		self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:0];
    }
    return self;
}
							
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
	CGRect frame = [self calculateWebViewFrame];
	UIWebView* webView = [[UIWebView alloc]initWithFrame:frame];
	webView.delegate = self;
	
	NSURLRequest* request = [NSURLRequest requestWithURL:[self requestURL]];
	[webView loadRequest:request];
	
	[self.view addSubview:webView];
	self.webView = webView;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (NSURL*)requestURL{
	return [NSURL URLWithString:kPOPULAR_URL];
}

-(BOOL)updateState{
	if(![self.webView canGoBack])
		self.navigationItem.leftBarButtonItem = nil;
	
	return YES;
}

-(void)back{
	if([_webView canGoBack])
		[_webView goBack];
	
	[self updateState];
}

#pragma mark - Private methods
-(CGRect)calculateWebViewFrame{
	CGRect bounds = self.view.bounds;
	CGRect tabRect = self.tabBarController.tabBar.frame;
	
	bounds.size.height -= CGRectGetHeight(tabRect);
	return bounds;
}

-(BOOL)shouldGoBack{
	if(!self.navigationItem.leftBarButtonItem &&
	   [_webView canGoBack])
		return YES;
	return NO;
}

-(UIBarButtonItem*)createBarButton{
	UIImage* image = [UIImage imageNamed:@"top-bar_backbutton"];
	CGSize size = image.size;
	UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[backButton setBackgroundImage:image forState:UIControlStateNormal];
	
	[backButton setFrame:CGRectMake(0, 0, size.width, size.height)];
	
	[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

	return [[UIBarButtonItem alloc] initWithCustomView:backButton];
}

#pragma mark - UIWebView delegate
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
	
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
	return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
	if([self shouldGoBack])
		self.navigationItem.leftBarButtonItem = [self createBarButton];
	
	if(![self.webView canGoBack])
		self.navigationItem.leftBarButtonItem = nil;
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
	
}

@end
